package br.com.persist.core;

import java.util.HashMap;
import java.util.Map;

import br.com.persist.api.ObjectNotFoundException;
import br.com.persist.api.VolatileContext;

public class DefaultVolatileContext implements VolatileContext {

	private Map<String, Object> values = new HashMap<String, Object>();

	public DefaultVolatileContext() {
		super();
	}

	public Object get(String key) throws ObjectNotFoundException {
		if (values.containsKey(key)) {
			return values.get(key);
		} else {
			throw new ObjectNotFoundException(key);
		}
	}

	public void update(String key, Object value) {
		values.put(key, value);
	}

	public VolatileContext createSubContext() {
		return new DefaultVolatileContext();
	}

	public void destroySubContext(VolatileContext subContext) {
		subContext.releaseAll();
	}

	public void release(String key) throws ObjectNotFoundException {
		if (values.containsKey(key)) {
			values.remove(key);
		} else {
			throw new ObjectNotFoundException(key);
		}
	}

	public void releaseAll() {
		values.clear();
		values = null;
	}
}
